import useUserStore from '@/store/modules/user'
import {menuSystem} from "@/utils/permissionSystem";
import {menuApprove} from "@/utils/permissionApprove";
import {menuBudget} from "@/utils/permissionBudget";
import {menuExpenditure} from "@/utils/permissionExpenditure";
import {menuContract} from "@/utils/permissionContract";
import {menuConstruction} from "@/utils/permissionConstruction";
import {menuPurchase} from "@/utils/permissionPurchase";
import {menuAsset} from "@/utils/permissionAssert";
import {isNullOrUndefinedOrEmpty} from "@/config/common";

export const always = 'no'

export function buildMenu(permissionCodes, code, name, path, redirect, component, title, icon, meta) {
    if (!isNullOrUndefinedOrEmpty(code) && code !== always && permissionCodes.indexOf(code) < 0) {
        component = 'error/403'
    }
    let o = {
        name,
        path,
        hidden: code ? permissionCodes.indexOf(code) < 0 : false,
        component,
        meta: {code, title, icon, noCache: false, link: null},
        children: []
    }
    if (redirect) {
        o.redirect = redirect
        o.alwaysShow = true
    }
    if (meta) {
        for (let k in meta) {
            o.meta[k] = meta[k]
        }
    }
    return o
}


export function allPermissions() {
    let permissionCodes = []
    useUserStore().permissions.map(item => {
        if (permissionCodes.indexOf(item.permissionCode) < 0) {
            permissionCodes.push(item.permissionCode)
        }
    })
    let all = []
    all.push(menuSystem(permissionCodes))
    // all.push(menuApprove(permissionCodes))
    all.push(menuBudget(permissionCodes))
    all.push(menuExpenditure(permissionCodes))
    all.push(menuContract(permissionCodes))
    // all.push(menuConstruction(permissionCodes))
    // all.push(menuPurchase(permissionCodes))
    // all.push(menuAsset(permissionCodes))
    return all
}
